package com.taiji.dicom4cloud.smart.alarm.utils;

import com.taiji.dicom4cloud.common.core.utils.CommonUtils;
import com.taiji.dicom4cloud.smart.alarm.common.AlarmTypeEnum;

import java.util.Arrays;

/**
 * @Description
 * @Author fanxl
 * @Date 2021/8/23
 **/
public class AlarmUtils {

    /**
     * 剩余电流
     */
    public static byte[] RESIDUAL_CURRENT = {0x01};

    /**
     * 温度
     */
    public static byte[] TEMPERATURE = {0x02};

    /**
     * 故障电弧
     */
    public static byte[] FAULT_ARC = {0x06};

    /**
     * 过流
     */
    public static byte[] OVERCURRENT = {0x07};

    /**
     * 短路
     */
    public static byte[] SHORT_CIRCUIT = {0x08};

    /**
     * 过压
     */
    public static byte[] OVERPRESSURE = {0x09};

    /**
     * 欠压
     */
    public static byte[] UNDERVOLTAGE = {0x0A};

    /**
     * 获取类型
     *
     * @param message
     * @return
     */
    public static byte[] getType(byte[] message) {
        return Arrays.copyOfRange(message, 0, 1);
    }

    public static Short getTypeShort(byte[] message) {
        byte data[] = new byte[2];
        data[0]=0x00;
        data[1] = Arrays.copyOfRange(message, 0, 1)[0];
        return CommonUtils.bytesToShort(data);
    }

    /**
     * 获取预警名称
     *
     * @param bytes
     * @return
     */
    public static String getAlarmName(byte[] bytes) {
        if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.RESIDUAL_CURRENT.getType())) {
            return AlarmTypeEnum.RESIDUAL_CURRENT.getInfo();
        } else if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.FAULT_ARC.getType())) {
            return AlarmTypeEnum.FAULT_ARC.getInfo();
        } else if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.OVERCURRENT.getType())) {
            return AlarmTypeEnum.OVERCURRENT.getInfo();
        } else if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.OVERPRESSURE.getType())) {
            return AlarmTypeEnum.OVERPRESSURE.getInfo();
        } else if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.TEMPERATURE.getType())) {
            return AlarmTypeEnum.TEMPERATURE.getInfo();
        } else if (MessageUtils.compere(getType(bytes),AlarmTypeEnum.UNDERVOLTAGE.getType())) {
            return AlarmTypeEnum.UNDERVOLTAGE.getInfo();
        }
        return null;
    }


    /**
     * 获取预警数据
     *
     * @param message
     * @return
     */
    public static byte[] getRealData(byte[] message) {
        return Arrays.copyOfRange(message, 1, 3);
    }

    /**
     * 获取设置数据
     *
     * @param message
     * @return
     */
    public static byte[] getSetData(byte[] message) {
        return Arrays.copyOfRange(message, 3, 5);
    }

    /**
     * 获取时间
     *
     * @param message
     * @return
     */
    public static byte[] getTime(byte[] message) {
        return Arrays.copyOfRange(message, 10, 14);
    }
}
